<?php
namespace Home\Controller;
require 'vendor/autoload.php';    // 注意位置一定要在 引入ThinkPHP入口文件 之前
use function PHPSTORM_META\map;
use Think\Controller;
vendor('PHPExcel.PHPExcel');
use JPush\Client as JPushClient;

class IndexController extends Controller {
     //首页
     public function index(){
        if(session('?username')){
            $info=M()->table('sdrksj')->field('id,cod,nh3-n,tp,tn,ss,hdsw,ssll,zrll,zljll,wt,scsj')->order('scsj desc')->find();
            $info1=M()->table('sdcksj')->field('id,cod,nh3-n,tp,tn,ssll,zrll,zljll,scsj')->order('scsj desc')->find();
            $info2=M()->table('szsj')->field('id,codmn,nh3-n,tp,tn,ph,do,cond,tub,wt,scsj')->order('scsj desc')->find();
            $info3=M()->table('qxsj')->field('id,wd,sd,fs,jyl,fx,scsj')->order('scsj desc')->find();
            if($info){
                $this->assign('rukou',$info);
            }
            if($info1){
                $this->assign('chukou',$info1);
            }
            if($info2){
                $this->assign('shuizhi',$info2);
            }
            if($info3){
                $this->assign('qixiang',$info3);
            }
            $this->display('index');
        }
        else{
            $this->login();
        }
    }
     public function shuaxin(){
         $info=M()->table('sdrksj')->field('scsj')->order('scsj desc')->find();
         $info1=M()->table('sdcksj')->field('scsj')->order('scsj desc')->find();
         $info2=M()->table('szsj')->field('scsj')->order('scsj desc')->find();
         $info3=M()->table('qxsj')->field('scsj')->order('scsj desc')->find();
         if($info['scsj']==$info1['scsj'] and $info2['scsj']==$info3['scsj'] and $info['scsj']==$info3['scsj']) {
             $dqsj = time();
             $shuaxin = (14400 - ($dqsj - $info['scsj'])) * 1000;
             if ($info) {
                 $this->ajaxreturn($shuaxin);
             }
         }
         else{
             $this->ajaxreturn('a');
         }
     }
     //跳转登陆
     public function login(){
        $this->display('login');
    }

    //验证登陆
    public function loginindex(){
        $username=$_POST['usr'];
        $password=$_POST['pwd'];

        $map['username']=$username;
        $map['password']=$password;
        // $map['role']='01';//限制其他人员登陆后台
        $map['_string']='(role=\'01\' or role=\'08\' or role=\'02\')';


        $info=M()->table('sd_user')->field('id,username,password,role')->where($map)->find();

        if($info){
            session('username',$info['username']);
            session('password',$info['password']);
            session('userid',$info['id']);
            session('role',$info['role']);
            $this->success('登陆成功','Index/index');
        }
        else{
            $this->error('请输入正确的用户名和密码！');
        }
    }

    //注销
    public function loginout(){
        //清空session
        session('username',null);
        session('password',null);
        $this->login();
    }


    //修改密码
    public function uppwd(){
        $o_pwd=$_POST['o_pwd'];
        $n_pwd=$_POST['n_pwd'];
        $username=session('username');

        $map['username']=$username;
        $map['password']=$o_pwd;

        $data['password']=$n_pwd;

        $info=M()->table('sd_user')->where($map)->save($data);

        if($info){
            $this->success('密码修改成功');
        }
        else{
            $this->error('密码修改失败，请稍后再试');
        }
    }
    //获取最后一条数据的上传时间，与当前时间相减结果为过多长时间后刷新网页
//    public function shuaxin(){
//        $scsj=M()->table('sdrksj')->field('scsj')->order('scsj desc')->find();
//        $dqsj=time();
//        $shuaxin=10000-($dqsj-$scsj);
//        if ($shuaxin < 0){
//            $shuaxin=null;
////        echo "<script>alert('已超过四小时未上传数据，请联系管理员');</script>";
//        }
//        return ($shuaxin);
//    }
    /**
     * 判断index页面的信号灯的颜色
     * 判断条件：1.数据库发来的数据不能为null
     * 2.四个小时是否有新的数据
     */
/**切记判断里用双等号，单等号是赋值*/
     public function xinhao1()
     {
         $info = M()->table('sdrksj')->field('id,cod,nh3-n,tp,tn,ss,hdsw,ssll,zrll,zljll,wt,scsj')->order('scsj desc')->find();
         $info1 = M()->table('sdcksj')->field('id,cod,nh3-n,tp,tn,ssll,zrll,zljll,scsj')->order('scsj desc')->find();
         $info2 = M()->table('szsj')->field('id,codmn,nh3-n,tp,tn,ph,do,cond,tub,wt,scsj')->order('scsj desc')->find();
         $info3 = M()->table('qxsj')->field('id,wd,sd,fs,jyl,fx,scsj')->order('scsj desc')->find();
         $pd = array('a' => 'isok', 'b' => 'isok', 'c' => 'isok', 'f' => 'isok');
         foreach ($info as $key => $value) {
             if ($value == null) {
                 $pd['a'] = 'notok';
                 break;
             }
         }
         foreach ($info1 as $key => $value) {
             if ($value == null) {
                 $pd['b'] = 'notok';;
                 break;
             }
         }
         foreach ($info2 as $key => $value) {
             if ($value == null) {
                 $pd['c'] = 'notok';
                 break;
             }
         }
         foreach ($info3 as $key => $value) {
             if ($value == null) {
                 $pd['f'] = 'notok';
                 break;
             }
         }
         $this->ajaxreturn($pd);
     }
    //用户管理
    public function yhgl(){
        if(session('role')=='01'){
            $map['pid']=5;
            $info=M()->table('sd_module')->where($map)->order('px')->select();
            if($info){
                $this->assign('menulist',$info);
                $this->assign('user',session('username'));
                $this->display('yhgl');
            }
        }
        else{
            $this->error('对不起！您没有此模块的操作权限...');
        }
    }
    //查询用户
    public function queryuser(){
        $username=$_POST['username'];
        if(!empty($username)){
            $map['username']=array('like','%'.$username.'%');
        }
        //计算总共有多少页
        $page=$_POST['page'];
        $rows=$_POST['rows'];
        $data=M()->table('sd_user')->field('id,username,password,sjzd(role,\'角色\') role,workpace,xm,phone,role as roleid')->where($map)->limit($rows*($page-1),$rows)->select();
        $total=M('sd_user')->where($map)->count();
        $count=array();
        $count['total']=$total;
        $count['rows']=$data;
        if($count){
            $this->ajaxreturn($count);
        }
    }
    //添加用户
    public function adduser(){
        $username=$_POST['username'];
        $password=$_POST['pwd'];
        $xm=$_POST['xm'];
        $phone=$_POST['phone'];
        $workpace=$_POST['workpace'];
        $role=$_POST['role'];

        $data['username']=$username;
        $data['password']=$password;
        $data['xm']=$xm;
        $data['phone']=$phone;
        $data['workpace']=$workpace;
        $data['role']=$role;

        $info=M()->table('sd_user')->data($data)->add();
        if($info>0){
            $this->ajaxreturn('添加用户成功！');
        }
        else{
            $this->ajaxreturn('添加用户失败！');
        }
    }
    //修改用户
    public function edituser(){
        $id=$_POST['hidid'];
        $username=$_POST['username'];
        $password=$_POST['pwd'];
        $xm=$_POST['xm'];
        $phone=$_POST['phone'];
        $workpace=$_POST['workpace'];
        $role=$_POST['role'];

        $map['id']=$id;

        $data['username']=$username;
        $data['password']=$password;
        $data['xm']=$xm;
        $data['phone']=$phone;
        $data['workpace']=$workpace;
        $data['role']=$role;

        $info=M()->table('sd_user')->where($map)->save($data);
        if($info>0){
            $this->ajaxreturn('修改用户成功！');
        }
        else{
            $this->ajaxreturn('修改用户失败！');
        }
    }
    //删除用户
    public function deluser(){
        $id=$_POST['id'];
        $map['id']=$id;
        $info= M()->table('sd_user')->where($map)->delete();
        if($info>0){
            $this->ajaxreturn('删除用户成功！');
        }
        else{
            $this->ajaxreturn('删除用户失败！');
        }
    }
    //湿地出口
    public function sdck(){
        $map['pid']=1;
        $info=M()->table('sd_module')->where($map)->order('px')->select();
        if($info){
            $this->assign('menulist',$info);
            $this->assign('user',session('username'));
            $this->display('sdck');
        }
    }
    public function querysdck(){
        $scsj=$_POST['scsj'];
        if(!empty($scsj)){
            $map['from_unixtime(scsj,\'%Y-%m-%d\')']=$scsj;
        }
            //计算总共有多少页
        $page=$_POST['page'];
        $rows=$_POST['rows'];
        $data=M()->table('sdcksj')->field('id,cod,nh3-n,tp,tn,ssll,zrll,zljll,from_unixtime(scsj,\'%Y-%m-%d %H:%i:%s\') scsj')->where($map)->order('scsj')->limit($rows*($page-1),$rows)->select();
        $total=M('sdcksj')->where($map)->count();
        $count=array();
        $count['total']=$total;
        $count['rows']=$data;
        $this->ajaxreturn($count);
    }
    public function delsdck(){
        if(session('role')=='01'){
            $id=$_POST['id'];
            $map['id']=$id;
            $info= M()->table('sdcksj')->where($map)->delete();
            if($info>0){
                $this->ajaxreturn('删除用户成功！');
            }
            else{
                $this->ajaxreturn('删除用户失败！');
            }
        }
        else{
            $this->error('对不起！您没有此功能的操作权限...');
        }
    }
    //湿地入口
    public function sdrk(){
        $map['pid']=1;
        $info=M()->table('sd_module')->where($map)->order('px')->select();
        if($info){
            $this->assign('menulist',$info);
            $this->assign('user',session('username'));
            $this->display('sdrk');
        }

    }
    public function querysdrk(){
        $scsj=$_POST['scsj'];
        if(!empty($scsj)){
            $map['from_unixtime(scsj,\'%Y-%m-%d\')']=$scsj;
        }
        //计算总共有多少页
        $page=$_POST['page'];
        $rows=$_POST['rows'];
        $data=M()->table('sdrksj')->field('id,cod,nh3-n,tp,tn,ss,hdsw,ssll,zrll,zljll,wt,from_unixtime(scsj,\'%Y-%m-%d %H:%i:%s\') scsj')->where($map)->order('scsj')->limit($rows*($page-1),$rows)->select();
        $total=M('sdrksj')->where($map)->count();
        $count=array();
        $count['total']=$total;
        $count['rows']=$data;
        $this->ajaxreturn($count);
    }
    public function delsdrk(){
        if(session('role')=='01'){
            $id=$_POST['id'];
            $map['id']=$id;
            $info= M()->table('sdrksj')->where($map)->delete();
            if($info>0){
                $this->ajaxreturn('删除用户成功！');
            }
            else{
                $this->ajaxreturn('删除用户失败！');
            }
        }
        else{
            $this->error('对不起！您没有此功能的操作权限...');
        }
    }
    //汶河水质
    public function whsz(){
        $map['pid']=1;
        $info=M()->table('sd_module')->where($map)->order('px')->select();
        if($info){
            $this->assign('menulist',$info);
            $this->assign('user',session('username'));
            $this->display('whsz');
        }
    }
    public function querywhsz(){
        $scsj=$_POST['scsj'];
        if(!empty($scsj)){
            $map['from_unixtime(scsj,\'%Y-%m-%d\')']=$scsj;
        }
        //计算总共有多少页
        $page=$_POST['page'];
        $rows=$_POST['rows'];
        $data=M()->table('szsj')->field('id,codmn,nh3-n,tp,tn,ph,do,cond,tub,wt,from_unixtime(scsj,\'%Y-%m-%d %H:%i:%s\') scsj')->where($map)->order('scsj')->limit($rows*($page-1),$rows)->select();
        $total=M('szsj')->where($map)->count();
        $count=array();
        $count['total']=$total;
        $count['rows']=$data;
        $this->ajaxreturn($count);
    }
    public function delwhsd(){
        if (session('role') == '01') {
            $id = $_POST['id'];
            $map['id'] = $id;
            $info = M()->table('szsj')->where($map)->delete();
            if ($info > 0) {
                $this->ajaxreturn('删除用户成功！');
            } else {
                $this->ajaxreturn('删除用户失败！');
            }
        } else {
            $this->error('对不起！您没有此功能的操作权限...');
        }
    }
    //气象数据
    public function qxsj(){
        $map['pid']=1;
        $info=M()->table('sd_module')->where($map)->order('px')->select();
        if($info){
            $this->assign('menulist',$info);
            $this->assign('user',session('username'));
            $this->display('qxsj');
        }
    }
    public function queryqxsj(){
        $scsj=$_POST['scsj'];
        if(!empty($scsj)){
            $map['from_unixtime(scsj,\'%Y-%m-%d\')']=$scsj;
        }
        //计算总共有多少页
        $page=$_POST['page'];
        $rows=$_POST['rows'];
        $data=M()->table('qxsj')->field('id,wd,sd,fs,jyl,fx,from_unixtime(scsj,\'%Y-%m-%d %H:%i:%s\') scsj')->where($map)->order('scsj')->limit($rows*($page-1),$rows)->select();
        $total=M('qxsj')->where($map)->count();
        $count=array();
        $count['total']=$total;
        $count['rows']=$data;
        $this->ajaxreturn($count);
    }
    public function delqxsj(){
        if (session('role') == '01') {
            $id = $_POST['id'];
            $map['id'] = $id;
            $info = M()->table('qxsj')->where($map)->delete();
            if ($info > 0) {
                $this->ajaxreturn('删除用户成功！');
            } else {
                $this->ajaxreturn('删除用户失败！');
            }
        } else {
            $this->error('对不起！您没有此功能的操作权限...');
        }
    }
    //导出湿地入口
    public function portrk(){
        $count=M()->table('sdrksj')->select();
        import("Org.Util.PHPExcel");
        import("Org.Util.PHPExcel.Worksheet.Drawing");
        import("Org.Util.PHPExcel.Writer.Excel2007");
        $objPHPExcel = new \PHPExcel();
        $objWriter = new \PHPExcel_Writer_Excel2007($objPHPExcel);
        $objActSheet = $objPHPExcel->getActiveSheet();

//设置标题栏
        $objActSheet->setCellValue('A1', '化学需氧量');
        $objActSheet->setCellValue('B1', '氨氮:');
        $objActSheet->setCellValue('C1', '总磷:');
        $objActSheet->setCellValue('D1', '总氮:');
        $objActSheet->setCellValue('E1', "悬浮物");
        $objActSheet->setCellValue('F1', "河道水位");
        $objActSheet->setCellValue('G1', "实时流量");
        $objActSheet->setCellValue('H1', "昨日流量");
        $objActSheet->setCellValue('I1', "总累计流量");
        $objActSheet->setCellValue('J1', "水温");
        $objActSheet->setCellValue('K1', "上传时间");
//把查询出来的数据循环写入excel
        foreach($count as $k => $v){
            $num=$k+2;
            $objActSheet->setCellValue('A'.$num, $v['cod']);
            $objActSheet->setCellValue('B'.$num, $v['nh3-n']);
            $objActSheet->setCellValue('C'.$num, $v['tp']);
            $objActSheet->setCellValue('D'.$num, $v['tn']);
            $objActSheet->setCellValue('E'.$num, $v['ss']);
            $objActSheet->setCellValue('F'.$num, $v['hdsw']);
            $objActSheet->setCellValue('G'.$num, $v['ssll']);
            $objActSheet->setCellValue('H'.$num, $v['zrll']);
            $objActSheet->setCellValue('I'.$num, $v['zljll']);
            $objActSheet->setCellValue('J'.$num, $v['wt']);
            $objActSheet->setCellValue('K'.$num, $v['scsj']);
        }

        $fileName = '湿地入口表';
        $date = date("Y-m-d",time());
        $fileName .= "_{$date}.xls";
        $fileName = iconv("utf-8", "gb2312", $fileName);

        $objPHPExcel->setActiveSheetIndex(0);
        ob_end_clean();//清除缓冲区,避免乱码
        header('Content-Type: application/vnd.ms-excel');
        header("Content-Disposition: attachment;filename=\"$fileName\"");
        header('Cache-Control: max-age=0');
        $objWriter = \PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5');
        $objWriter->save('php://output'); //文件通过浏览器下载
    }
    //导出湿地出口
    public function portck(){
        $count=M()->table('sdcksj')->select();
        import("Org.Util.PHPExcel");
        import("Org.Util.PHPExcel.Worksheet.Drawing");
        import("Org.Util.PHPExcel.Writer.Excel2007");
        $objPHPExcel = new \PHPExcel();
        $objWriter = new \PHPExcel_Writer_Excel2007($objPHPExcel);
        $objActSheet = $objPHPExcel->getActiveSheet();

//设置标题栏
        $objActSheet->setCellValue('A1', '化学需氧量');
        $objActSheet->setCellValue('B1', '氨氮:');
        $objActSheet->setCellValue('C1', '总磷:');
        $objActSheet->setCellValue('D1', '总氮:');
        $objActSheet->setCellValue('E1', "实时流量");
        $objActSheet->setCellValue('F1', "昨日流量");
        $objActSheet->setCellValue('G1', "总累计流量");
        $objActSheet->setCellValue('H1', "上传时间");
//把查询出来的数据循环写入excel
        foreach($count as $k => $v){
            $num=$k+2;
            $objActSheet->setCellValue('A'.$num, $v['cod']);
            $objActSheet->setCellValue('B'.$num, $v['nh3-n']);
            $objActSheet->setCellValue('C'.$num, $v['tp']);
            $objActSheet->setCellValue('D'.$num, $v['tn']);
            $objActSheet->setCellValue('E'.$num, $v['ssll']);
            $objActSheet->setCellValue('F'.$num, $v['zrll']);
            $objActSheet->setCellValue('G'.$num, $v['zljll']);
            $objActSheet->setCellValue('H'.$num, $v['scsj']);

        }

        $fileName = '湿地出口表';
        $date = date("Y-m-d",time());
        $fileName .= "_{$date}.xls";
        $fileName = iconv("utf-8", "gb2312", $fileName);

        $objPHPExcel->setActiveSheetIndex(0);
        ob_end_clean();//清除缓冲区,避免乱码
        header('Content-Type: application/vnd.ms-excel');
        header("Content-Disposition: attachment;filename=\"$fileName\"");
        header('Cache-Control: max-age=0');
        $objWriter = \PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5');
        $objWriter->save('php://output'); //文件通过浏览器下载
    }
    //导出汶河水质
    public function portsz(){
        $count=M()->table('szsj')->select();
        import("Org.Util.PHPExcel");
        import("Org.Util.PHPExcel.Worksheet.Drawing");
        import("Org.Util.PHPExcel.Writer.Excel2007");
        $objPHPExcel = new \PHPExcel();
        $objWriter = new \PHPExcel_Writer_Excel2007($objPHPExcel);
        $objActSheet = $objPHPExcel->getActiveSheet();

//设置标题栏
        $objActSheet->setCellValue('A1', '高锰酸盐指数');
        $objActSheet->setCellValue('B1', '氨氮:');
        $objActSheet->setCellValue('C1', '总磷:');
        $objActSheet->setCellValue('D1', '总氮:');
        $objActSheet->setCellValue('E1', "PH");
        $objActSheet->setCellValue('F1', "熔接氮");
        $objActSheet->setCellValue('G1', "电导率");
        $objActSheet->setCellValue('H1', "浊度");
        $objActSheet->setCellValue('I1', "水温");
        $objActSheet->setCellValue('J1', "上传时间");
//把查询出来的数据循环写入excel
        foreach($count as $k => $v){
            $num=$k+2;
            $objActSheet->setCellValue('A'.$num, $v['codmn']);
            $objActSheet->setCellValue('B'.$num, $v['nh3-n']);
            $objActSheet->setCellValue('C'.$num, $v['tp']);
            $objActSheet->setCellValue('D'.$num, $v['tn']);
            $objActSheet->setCellValue('E'.$num, $v['ph']);
            $objActSheet->setCellValue('F'.$num, $v['do']);
            $objActSheet->setCellValue('G'.$num, $v['cond']);
            $objActSheet->setCellValue('H'.$num, $v['tub']);
            $objActSheet->setCellValue('I'.$num, $v['wt']);
            $objActSheet->setCellValue('J'.$num, $v['scsj']);
        }

        $fileName = '湿地水质表';
        $date = date("Y-m-d",time());
        $fileName .= "_{$date}.xls";
        $fileName = iconv("utf-8", "gb2312", $fileName);

        $objPHPExcel->setActiveSheetIndex(0);
        ob_end_clean();//清除缓冲区,避免乱码
        header('Content-Type: application/vnd.ms-excel');
        header("Content-Disposition: attachment;filename=\"$fileName\"");
        header('Cache-Control: max-age=0');
        $objWriter = \PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5');
        $objWriter->save('php://output'); //文件通过浏览器下载
    }
    //导出气象数据
    public function portqx(){
        $count=M()->table('szsj')->select();
        import("Org.Util.PHPExcel");
        import("Org.Util.PHPExcel.Worksheet.Drawing");
        import("Org.Util.PHPExcel.Writer.Excel2007");
        $objPHPExcel = new \PHPExcel();
        $objWriter = new \PHPExcel_Writer_Excel2007($objPHPExcel);
        $objActSheet = $objPHPExcel->getActiveSheet();

//设置标题栏
        $objActSheet->setCellValue('A1', '温度：');
        $objActSheet->setCellValue('B1', '湿度:');
        $objActSheet->setCellValue('C1', '风速:');
        $objActSheet->setCellValue('D1', '降雨量:');
        $objActSheet->setCellValue('E1', "风向");
        $objActSheet->setCellValue('F1', "上传时间");
//把查询出来的数据循环写入excel
        foreach($count as $k => $v){
            $num=$k+2;
            $objActSheet->setCellValue('A'.$num, $v['wd']);
            $objActSheet->setCellValue('B'.$num, $v['sd']);
            $objActSheet->setCellValue('C'.$num, $v['fs']);
            $objActSheet->setCellValue('D'.$num, $v['jyl']);
            $objActSheet->setCellValue('E'.$num, $v['fx']);
            $objActSheet->setCellValue('F'.$num, $v['scsj']);
        }

        $fileName = '气象数据表';
        $date = date("Y-m-d",time());
        $fileName .= "_{$date}.xls";
        $fileName = iconv("utf-8", "gb2312", $fileName);

        $objPHPExcel->setActiveSheetIndex(0);
        ob_end_clean();//清除缓冲区,避免乱码
        header('Content-Type: application/vnd.ms-excel');
        header("Content-Disposition: attachment;filename=\"$fileName\"");
        header('Cache-Control: max-age=0');
        $objWriter = \PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5');
        $objWriter->save('php://output'); //文件通过浏览器下载
    }
}